<template>
  <div class="left-box">
    <van-list
      v-model="$store.state.FirstPageModule.loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
    >
      <van-cell
        class="mylist"
        v-for="data in filmList"
        :key="data.filmId"
        @click="itemClick(data)"
      >
        <img :src="data.poster" alt="图片" />
        <div class="text-content">
          <h6>
            {{ data.name }} <span class="item">{{ data.filmType.name }}</span>
          </h6>
          <p>观众评分{{ data.grade }}</p>
          <p class="actors">主演：{{ data.actors | actorsFilter }}</p>
          <p>{{ data.nation }} | {{ data.runtime }}分钟</p>
        </div>
      </van-cell>
    </van-list>
  </div>
</template>
<script>
import { List, Cell } from "vant";
import { mapActions, mapState, mapMutations } from "vuex";
import Vue from "vue";
Vue.use(List).use(Cell);
Vue.filter("actorsFilter", (actors) => {
  if (actors === undefined) {
    return "暂无主演";
  }
  return actors.map((item) => item.name).join(" ");
});
export default {
  name: "PageFirst",
  data() {
    return {
      pageNum: 0,
    };
  },
  computed: {
    ...mapState("FirstPageModule", ["finished", "filmList", "firstNum"]),
  },
  methods: {
    ...mapMutations("FirstPageModule", [
      "setFirstNum",
      "setFilmList",
    ]),
    ...mapActions("FirstPageModule", ["getFilmList"]),
    getObjParams(cityId, pageNum) {
      let obj = { cityId, pageNum };
      return obj;
    },
    itemClick(data) {
      //路由跳转的方式
      //路径方式
      this.$router.push({ path: `/detail/${data.filmId}` }); //编程式导航
      //this.$route

      //名字
      // this.$router.push({
      //   name: "Detail",
      //   params: {
      //     id: data.filmId,
      //   },
      // });
      //this.$route.params.myid

      // Query方式
      // this.$router.push(`/detail?id=${data.filmId}`);
      //this.$route.quary.id
    },
    getData() {
      this.pageNum++;
      this.getFilmList(
        this.getObjParams(this.$store.state.cityId, this.pageNum)
      ).then(() => {
        this.setFirstNum(this.pageNum);
      });
    },
    onLoad() {
      if (this.finished) {
        return;
      }
      this.getData();
    },
  },
  mounted() {
    this.pageNum++;
    if (this.$store.state.firstCityId != this.$store.state.cityId) {
      this.setFilmList([]);
      this.getFilmList(
        this.getObjParams(this.$store.state.cityId, this.pageNum)
      ).then(() => {
        this.setFirstNum(this.pageNum);
        this.$store.commit("firstCityId", this.$store.state.cityId);
      });
    } else {
      if (this.filmList.length === 0) {
        this.getFilmList(
          this.getObjParams(this.$store.state.cityId, this.pageNum)
        ).then(() => {
          this.setFirstNum(this.pageNum);
        });
      } else {
        this.pageNum = this.firstNum;
        console.log("已经从缓存中获取数据");
      }
    }
  },
};
</script>
<style lang="less" scoped>
.left-box {
  width: 100px;
  margin: 0 atuo;
  margin-bottom: 50px;
  span {
    font-size: 40px;
  }
  button {
    width: 100px;
    height: 50px;
  }
  .mylist {
    padding: 5px;
    img {
      float: left;
      width: 66px;
    }
    .text-content {
      margin-left: 10px;
      float: left;
      text-align: left;
      .actors {
        overflow: hidden; //隐藏文字
        text-overflow: ellipsis; //显示 ...
        white-space: nowrap; //不换行
        width: 250px;
      }
      p {
        color: #797d82;
        font-size: 13px;
        margin-top: 4px;
      }
      .item {
        font-size: 9px;
        color: #fff;
        background-color: #d2d6dc;
        height: 14px;
        line-height: 14px;
        padding: 0 2px;
        border-radius: 2px;
      }
    }
  }
}
</style>